import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:mahjong/const/LocationEnum.dart';
import 'package:mahjong/entity/MessageContext.dart';
import 'package:mahjong/entity/mahjong/MahjongInfoEntity.dart';
import 'package:mahjong/entity/user/UserLocation.dart';
import 'package:mahjong/notify/CurrentUserInfo.dart';
import 'package:mahjong/notify/LeftUserInfo.dart';
import 'package:mahjong/notify/RightUserInfo.dart';
import 'package:mahjong/notify/TopUserInfo.dart';
import 'package:provider/provider.dart';

import 'AbstractMessageHandler.dart';
import 'package:mahjong/const/MahjongConst.dart';

class MessageReadHomeHandler extends AbstractMessageHandler {
  void doMessage(MessageContext message) {
    userLocation.clear();
    MahjongInfoEntity roomInfo = Provider.of<MahjongInfoEntity>(
        navigatorKey.currentContext,
        listen: false);
    diskPartUserProvider.forEach((key, value) {
      value.cleanUserInfo();
    });
    roomInfo.updateInfo(MahjongInfoEntity.fromJson(message.body));
    CurrentUserInfo user = Provider.of<CurrentUserInfo>(
        navigatorKey.currentContext,
        listen: false);
    // 初始化用户的手牌
    user.initCurrentInfo(message.body, roomInfo.currentExportUserId);
    userLocation[user.userId] = UserLocation.init(Location.BOTTOM, GlobalKey());
    diskPartUserProvider[user.userId] = Provider.of<CurrentUserInfo>(
        navigatorKey.currentContext,
        listen: false);
    int userCount = roomInfo.rule.userCount;
    roomInfo.allUserMap.forEach((userId, userInfo) {
      if (userCount == 2 && userId != user.userId) {
        var topUser = Provider.of<TopUserInfo>(navigatorKey.currentContext,
            listen: false);
        diskPartUserProvider[userId] = topUser;
        userLocation[userId] = UserLocation.init(Location.TOP, GlobalKey());
        topUser.initOtherUser(
            message.body, userInfo, roomInfo.currentExportUserId);
      }

      if (userCount == 3 && userId != user.userId) {
        if (subtraction(userCount, user.seq) == userInfo.seq) {
          var leftUser = Provider.of<LeftUserInfo>(navigatorKey.currentContext,
              listen: false);
          diskPartUserProvider[userId] = leftUser;
          userLocation[userId] = UserLocation.init(Location.LEFT, GlobalKey());
          leftUser.initOtherUser(
              message.body, userInfo, roomInfo.currentExportUserId);
        } else if (addition(userCount, user.seq) == userInfo.seq) {
          var topUser = Provider.of<TopUserInfo>(navigatorKey.currentContext,
              listen: false);
          diskPartUserProvider[userId] = topUser;
          userLocation[userId] = UserLocation.init(Location.TOP, GlobalKey());
          topUser.initOtherUser(
              message.body, userInfo, roomInfo.currentExportUserId);
        }
      }

      if (userCount == 4 && userId != user.userId) {
        if (subtraction(userCount, user.seq) == userInfo.seq) {
          var rightUser = Provider.of<RightUserInfo>(
              navigatorKey.currentContext,
              listen: false);
          diskPartUserProvider[userId] = rightUser;
          userLocation[userId] = UserLocation.init(Location.RIGHT, GlobalKey());
          rightUser.initOtherUser(
              message.body, userInfo, roomInfo.currentExportUserId);
        } else if (addition(userCount, user.seq) == userInfo.seq) {
          var leftUser = Provider.of<LeftUserInfo>(navigatorKey.currentContext,
              listen: false);
          diskPartUserProvider[userId] = leftUser;
          userLocation[userId] = UserLocation.init(Location.LEFT, GlobalKey());
          leftUser.initOtherUser(
              message.body, userInfo, roomInfo.currentExportUserId);
        } else {
          var topUser = Provider.of<TopUserInfo>(navigatorKey.currentContext,
              listen: false);
          diskPartUserProvider[userId] = topUser;
          userLocation[userId] = UserLocation.init(Location.TOP, GlobalKey());
          topUser.initOtherUser(
              message.body, userInfo, roomInfo.currentExportUserId);
        }
      }
    });
  }

  //  决定玩家是哪个位置

  int subtraction(int count, int current) {
    if (current + 1 > count) {
      return 1;
    }
    return current + 1;
  }

  int addition(int count, int current) {
    if (current - 1 <= 0) {
      return count;
    }
    return current - 1;
  }
}
